package View;


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import Controller.ServerController;


public class ServerMainFrame extends JFrame{
	
	final JLabel nameTopic = new JLabel("Name/topic :");
	final JLabel userConnected = new JLabel("Users connected :");
	final JLabel newChatRoom = new JLabel("Create a new chat room :");
	
	final JButton connect = new JButton("Connect");
	final JButton delete = new JButton("Delete");
	final JButton create = new JButton("Create");
	final JButton disconnect = new JButton("Disconnect user");
	
	JTextField newChatRoomName= new JTextField();
	
	// The list of the chat rooms
    ArrayList<String> chatRoom = new ArrayList<String>();
    
    // The list of the chat rooms
    ArrayList<String> users = new ArrayList<String>();
    
	JPanel contentPane = new JPanel();
	
	JLabel chatRoomName;

	ServerController serverController;
	
	public ServerMainFrame()
	{				
		//Create the ServerController 
		serverController = new ServerController();
		
		this.paintFrame();
	}

	public void paintFrame(){
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setSize(1000, 700);
		setTitle("Server");	
		
		// Aller chercher les chats room 
	    chatRoom.add("ChatRoom1");
	    chatRoom.add("ChatRoom");
		
		JList list = new JList(chatRoom.toArray());
		
		contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
		contentPane.setLayout(new BorderLayout(10, 10));
		setContentPane(contentPane);
		
		//contentPane.add(welcomeMessage, BorderLayout.NORTH);
		
		
		
		/////////////////////// Chat rooms ////////////////////////////////////////
		
		
		JPanel chat = new JPanel();
		chat.setLayout(new BorderLayout());
		
		JScrollPane scrollPane = new JScrollPane(list);
		scrollPane.setBorder(new TitledBorder(new EtchedBorder(), "Available chat rooms : "));
		//scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setPreferredSize(new Dimension(200,100)); 

		contentPane.add(scrollPane, BorderLayout.WEST);
		
		
		
		
		////////////// Commands ///////////////////////////////////////
		
		JPanel commands = new JPanel();
		contentPane.add(commands, BorderLayout.CENTER);
		
		
		///// Chat Room information 
		
		JPanel chatRoomInfo = new JPanel();
		chatRoomInfo.setLayout(new BorderLayout());
		chatRoomInfo.setBorder(new TitledBorder(new EtchedBorder(),
				"Information about the room : "));
		chatRoomInfo.setPreferredSize(new Dimension(550,200)); 
		
		// Add the name of the selected chat room
		JPanel info = new JPanel();
		info.setBorder(new EmptyBorder(10, 10, 0, 0));
		
		info.setLayout(new BorderLayout(0,-50));
		chatRoomInfo.add(info, BorderLayout.CENTER);
		
		info.add(nameTopic, BorderLayout.NORTH);
		chatRoomName = new JLabel();
		
		info.add(userConnected, BorderLayout.CENTER);

		
		///// Connect to the chat room 
	
			
		JPanel flowPanel = new JPanel(new FlowLayout());
		flowPanel.add(connect);
		flowPanel.add(delete);
		chatRoomInfo.add(flowPanel, BorderLayout.SOUTH);

		commands.add(chatRoomInfo, BorderLayout.NORTH);
		
		
		////////////// Open a new chat room //////////////////////////////////////////
		
		JPanel newPanelChatRoom = new JPanel();
		newPanelChatRoom.setPreferredSize(new Dimension(550,30));
		newPanelChatRoom.setLayout(new BorderLayout(0,0));

		newPanelChatRoom.add(newChatRoom, BorderLayout.WEST);
		newPanelChatRoom.add(newChatRoomName, BorderLayout.CENTER);
		newPanelChatRoom.add(create, BorderLayout.EAST);
		
		commands.add(newPanelChatRoom, BorderLayout.CENTER);
		
		
		
		
		////////// Console //////////////////


		JPanel consoleButton = new JPanel();
		consoleButton.setLayout(new BorderLayout());
		
		JPanel console = new JPanel();
		console.setPreferredSize(new Dimension(550,387));
		
		console.setLayout(new BorderLayout());
		console.setBorder(new TitledBorder(new EtchedBorder(), "Console : "));
		
		consoleButton.add(console, BorderLayout.CENTER);
		
		commands.add(consoleButton, BorderLayout.SOUTH);
		

		JScrollPane consoleScroll = new JScrollPane();
		consoleScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

		console.add(consoleScroll, BorderLayout.CENTER);


		
		
		/////////////////////// Users connected ////////////////////////////////////////
		
	    users.add("User1");
	    users.add("User2");
		
		JList list1 = new JList(users.toArray());
		
		JPanel users = new JPanel();
		users.setLayout(new BorderLayout());

		JScrollPane scrollPane1 = new JScrollPane(list1);
		scrollPane1.setBorder(new TitledBorder(new EtchedBorder(), "Users : "));
		//scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane1.setPreferredSize(new Dimension(200,100)); 
	

		users.add(scrollPane1, BorderLayout.CENTER);
		
		JPanel flowPanel1 = new JPanel(new FlowLayout());
		flowPanel1.add(disconnect);
		users.add(flowPanel1, BorderLayout.SOUTH);
		
		users.add(disconnect, BorderLayout.SOUTH);
		
		contentPane.add(users, BorderLayout.EAST);
	
		setLocationRelativeTo(null);

		setVisible(true);
		
		
	}
	
	
	public static void main(String argv[])
	{
		ServerMainFrame clientMain = new ServerMainFrame();
	}
	


}
